//
//  HTFindGoodsViewController.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/16.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTFindGoodsViewController.h"
#import "HTGoodsSearchVC.h"
#import "HTGoodsSpecialVC.h"

@interface HTFindGoodsViewController ()

@property (nonatomic,strong) HTGoodsSearchVC *search;
@property (nonatomic,strong) HTGoodsSpecialVC *special;
@property (nonatomic,weak) UIViewController *currentVC;


@end

@implementation HTFindGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNav];
    [self configSubViews];
}
- (void)configSubViews{
    self.special = [[HTGoodsSpecialVC alloc]init];
    self.special.view.frame = CGRectMake(0, 0, kHTScreenWidth, kHTScreenHeight);
    [self addChildViewController:self.special];
    self.search = [[HTGoodsSearchVC alloc]init];
    self.search.view.frame = CGRectMake(0, 0, kHTScreenWidth, kHTScreenHeight);
    [self addChildViewController:self.search];
    //设置默认控制器为fristVc
    self.currentVC = self.search;
    [self.view addSubview:self.search.view];
}
- (void)configNav{
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"货源专线",@"货源搜索"]];
    [seg setSelectedSegmentIndex:1];
    [seg addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    seg.backgroundColor = [UIColor clearColor];
    seg.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = seg;
}

- (void)segmentedControlAction:(UISegmentedControl *)sender{
    switch (sender.selectedSegmentIndex) {
        case 0:
            [self replaceFromOldViewController:self.search toNewViewController:self.special];
            break;
        case 1:
            [self replaceFromOldViewController:self.special toNewViewController:self.search];
            break;
        default:
            break;
    }
}
- (void)replaceFromOldViewController:(UIViewController *)oldVc toNewViewController:(UIViewController *)newVc{
    [self addChildViewController:newVc];
    [self transitionFromViewController:oldVc toViewController:newVc duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newVc didMoveToParentViewController:self];
            [oldVc willMoveToParentViewController:nil];
            [oldVc removeFromParentViewController];
            self.currentVC = newVc;
        }else{
            self.currentVC = oldVc;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
